package com.adtg.peru;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.IntentAction;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SocialActivity extends Activity {

	private static final String CONSUMER_KEY = "d3WPIS7eXWcwuqjR3Uuw";
	private static final String CONSUMER_SECRET = "4u9bTWleNlpHdifkSpDoZp1AKhwGr0I0uikQp1532w";
	private static final String REQUEST_URL = "https://api.twitter.com/oauth/request_token";
	private static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize";
	private static final String ACCESS_URL = "https://api.twitter.com/oauth/access_token";
	private static final String CALLBACK_URL = "app://goingsocial";

	private TextView codigoRespuesta;
	private Button botonAutorizar;
	private EditText campoTweet;
	private Button botonEnviarTweet;

	private CommonsHttpOAuthConsumer httpOauthConsumer;
	private OAuthProvider httpOauthprovider;
	private AccessToken accessToken;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.social_twitter);

		ActionBar actionbar = (ActionBar) findViewById(R.id.actionbar);

		actionbar.setTitle("Social");
		actionbar.setHomeAction(new IntentAction(SocialActivity.this,
				HomeActivity.createIntent(this), R.drawable.ic_title_home2));
		actionbar.setDisplayHomeAsUpEnabled(true);

		codigoRespuesta = (TextView) findViewById(R.id.codigoRespuesta);
		botonAutorizar = (Button) findViewById(R.id.botonAutorizar);
		campoTweet = (EditText) findViewById(R.id.tweet);
		botonEnviarTweet = (Button) findViewById(R.id.botonEnviarTweet);

		botonAutorizar.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				autoriza();
			}
		});

		botonEnviarTweet.setOnClickListener(new View.OnClickListener() {
			// @Override
			public void onClick(View v) {
				enviaTweet();
			}
		});
	}

	private void autoriza() {

		try {

			httpOauthConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
					CONSUMER_SECRET);

			httpOauthprovider = new DefaultOAuthProvider(REQUEST_URL,
					ACCESS_URL, AUTHORIZE_URL);

			String authUrl = httpOauthprovider.retrieveRequestToken(
					httpOauthConsumer, CALLBACK_URL);

			this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
					.parse(authUrl)));

		} catch (Exception e) {

			codigoRespuesta.setText(e.getMessage());

		}

	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		Uri uri = intent.getData();
		if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
			String verifier = uri
					.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
			try {
				httpOauthprovider.retrieveAccessToken(httpOauthConsumer,
						verifier);
				accessToken = new AccessToken(httpOauthConsumer.getToken(),
						httpOauthConsumer.getTokenSecret());

				botonAutorizar.setEnabled(false);
			} catch (Exception e) {
				codigoRespuesta.setText(e.getMessage());
			}
		}
	}

	private void enviaTweet() {
		try {
			Twitter twitter = new TwitterFactory().getInstance();
			twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
			twitter.setOAuthAccessToken(accessToken);

			twitter.updateStatus(campoTweet.getText().toString());

			codigoRespuesta.setText("Tweet enviado!");
			EditText txtTwit = (EditText) findViewById(R.id.tweet);
			txtTwit.setText("#io12Peru #gadcLima #academictg #gdgLima");
			// txtTwit.setHint("Escribe tu mensaje aqui");
		} catch (Exception e) {
			codigoRespuesta.setText(e.getMessage());
		}
	}

}
